/*****************************************************************************************
 * FILE NAME: drv_usart                       COPYRIGHT (c) cos 2024
 * REVISION:  1.0.0                                     All Rights Reserved
 * DESCRIPTION:
 * =======================================================================================
 * UPDATE HISTORY：
 * REV      AUTHOR      DATE
 * v1.0.0   Tuxin       2024.10.14
 * DESCRIPTION OF CHANGE:
 ****************************************************************************************/
#ifndef __DRV_USART_H
#define __DRV_USART_H

//----------------------------------------------------------------------------------------
// @Project Includes
#include "typedefs.h"

//----------------------------------------------------------------------------------------
// @Global Macro Define in the module
#define COMn                            (4U)

/*串口1配置相关*/
/* USART channel definition */
#define USART1_CH                       (USART1)
/* USART baudrate definition */
#define USART1_BAUDRATE                 (115200ul)
#define USART1_CLK          						RCC_APB2_PERIPH_USART1
#define USART1_GPIO_CLK     						RCC_APB2_PERIPH_GPIOA
/* USART RX Port/Pin definition */
#define USART1_RX_PORT                  (GPIOA)
#define USART1_RX_PIN                   (GPIO_PIN_10)
/* USART TX Port/Pin definition */
#define USART1_TX_PORT                  (GPIOA)
#define USART1_TX_PIN                   (GPIO_PIN_9)

#define USART1_Rx_GPIO_AF   						(GPIO_AF4_USART1)
#define USART1_Tx_GPIO_AF   						(GPIO_AF4_USART1)
#define USART1_APBxClkCmd   						RCC_EnableAPB2PeriphClk
#define USART1_IRQn         						(USART1_IRQn)

/*串口3配置相关*/
/* USART channel definition */
#define USART3_CH                       (USART3)
/* USART baudrate definition */
#define USART3_BAUDRATE                 (9600ul)
#define USART3_CLK          						RCC_APB1_PERIPH_USART3
#define USART3_GPIO_CLK     						RCC_APB2_PERIPH_GPIOB
/* USART RX Port/Pin definition */
#define USART3_RX_PORT                  (GPIOB)
#define USART3_RX_PIN                   (GPIO_PIN_11)
/* USART TX Port/Pin definition */
#define USART3_TX_PORT                  (GPIOB)
#define USART3_TX_PIN                   (GPIO_PIN_10)

#define USART3_Rx_GPIO_AF   						(GPIO_AF5_USART3)
#define USART3_Tx_GPIO_AF   						(GPIO_AF0_USART3)
#define USART3_APBxClkCmd   						RCC_EnableAPB1PeriphClk
#define USART3_IRQn         						(USART3_IRQn)

/*串口4配置相关*/
/* USART channel definition */
#define USART4_CH                       (UART4)
/* USART baudrate definition */
#define USART4_BAUDRATE                 (9600ul)
#define USART4_CLK          						RCC_APB2_PERIPH_UART4
#define USART4_GPIO_CLK     						RCC_APB2_PERIPH_GPIOB
/* USART RX Port/Pin definition */
#define USART4_RX_PORT                  (GPIOB)
#define USART4_RX_PIN                   (GPIO_PIN_1)
/* USART TX Port/Pin definition */
#define USART4_TX_PORT                  (GPIOB)
#define USART4_TX_PIN                   (GPIO_PIN_0)

#define USART4_Rx_GPIO_AF   						(GPIO_AF6_UART4)
#define USART4_Tx_GPIO_AF   						(GPIO_AF6_UART4)
#define USART4_APBxClkCmd   						RCC_EnableAPB2PeriphClk
#define USART4_IRQn         						(UART4_IRQn)


/*串口5配置相关*/
/* USART channel definition */
#define USART5_CH                       (UART5)
/* USART baudrate definition */
#define USART5_BAUDRATE                 (115200ul)
#define USART5_CLK          						RCC_APB2_PERIPH_UART5
#define USART5_GPIO_CLK     						RCC_APB2_PERIPH_GPIOB
///* USART RX Port/Pin definition */
//#define USART5_RX_PORT                  (GPIOB)
//#define USART5_RX_PIN                   (GPIO_PIN_9)
/* USART TX Port/Pin definition */
#define USART5_TX_PORT                  (GPIOB)
#define USART5_TX_PIN                   (GPIO_PIN_8)

#define USART5_Rx_GPIO_AF   						(GPIO_AF6_UART5)
#define USART5_Tx_GPIO_AF   						(GPIO_AF6_UART5)
#define USART5_APBxClkCmd   						RCC_EnableAPB2PeriphClk
#define USART5_IRQn         						(UART5_IRQn)

#define USART_LOG_MAX_RX_SIZE						(10)
#define USART_LOG_MAX_TX_SIZE						(1024)/*串口LOG*/
#define USART_RS485_MAX_RX_SIZE					(512+32)
#define USART_RS485_MAX_TX_SIZE					(512+32)/*串口RS485*/
#define USART_SIF1_MAX_RX_SIZE					(512+32)
#define USART_SIF1_MAX_TX_SIZE					(512+32)/*串口SIF1*/
#define USART_SIF2_MAX_RX_SIZE					(512+32)
#define USART_SIF2_MAX_TX_SIZE					(512+32)/*串口SIF1*/

#define USART_LOG_INDEX				    			(5)
#define USART_RS485_INDEX			    			(1)
#define USART_SIF1_INDEX				    		(3)
#define USART_SIF2_INDEX				    		(4)


#define USART_TX_COMPLETE_MAX_TIMEOUT   (2)/*串口延时解析时间 20ms*/
//----------------------------------------------------------------------------------------
// @Global Type defines in the module
typedef struct stc_uart_log
{    
    bool_t btxEnable;/*中断发送开关*/
    vuint32_t vu32txMQPushLen;/*发送写入消息*/
    vuint32_t vu32txMQPopLen;/*发送弹出消息*/
    uint8_t u8txbuff[USART_LOG_MAX_TX_SIZE];
	  uint8_t u8txCompleteTimeout;/*发送完成超时检测溢出时间，485独有*/

    vuint8_t vu8rxData ;/*接收串口数据*/		
    vuint32_t vu32rxMQPush;/*发送写入消息*/
    uint8_t u8rxbuff[USART_LOG_MAX_RX_SIZE];	

    bool_t brxCompleteFlg;/*启动接收标志*/	
} stc_uart_log_t;

typedef struct stc_uart_rs485
{    
    bool_t btxEnable;/*中断发送开关*/
    vuint32_t vu32txMQPushLen;/*发送写入消息*/
    vuint32_t vu32txMQPopLen;/*发送弹出消息*/
    uint8_t u8txbuff[USART_RS485_MAX_TX_SIZE];
	  uint8_t u8txCompleteTimeout;/*发送完成超时检测溢出时间，485独有*/

    vuint8_t vu8rxData ;/*接收串口数据*/		
    vuint32_t vu32rxMQPush;/*发送写入消息*/
    uint8_t u8rxbuff[USART_RS485_MAX_RX_SIZE];

//    vuint8_t vu8rxBackData ;/*接收串口数据*/		
    vuint32_t vu32rxBackMQPush;/*发送写入消息*/
    uint8_t u8rxBackbuff[USART_RS485_MAX_RX_SIZE];		

    bool_t brxCompleteFlg;/*启动接收标志*/	
} stc_uart_rs485_t;

typedef struct stc_uart_sif1
{    
    bool_t btxEnable;/*中断发送开关*/
    vuint32_t vu32txMQPushLen;/*发送写入消息*/
    vuint32_t vu32txMQPopLen;/*发送弹出消息*/
    uint8_t u8txbuff[USART_SIF1_MAX_TX_SIZE];
	  uint8_t u8txCompleteTimeout;/*发送完成超时检测溢出时间，485独有*/

    vuint8_t vu8rxData ;/*接收串口数据*/		
    vuint32_t vu32rxMQPush;/*发送写入消息*/
    uint8_t u8rxbuff[USART_SIF1_MAX_RX_SIZE];	

    bool_t brxCompleteFlg;/*启动接收标志*/	
} stc_uart_sif1_t;

typedef struct stc_uart_sif2
{    
    bool_t btxEnable;/*中断发送开关*/
    vuint32_t vu32txMQPushLen;/*发送写入消息*/
    vuint32_t vu32txMQPopLen;/*发送弹出消息*/
    uint8_t u8txbuff[USART_SIF2_MAX_TX_SIZE];
	  uint8_t u8txCompleteTimeout;/*发送完成超时检测溢出时间，485独有*/

    vuint8_t vu8rxData ;/*接收串口数据*/		
    vuint32_t vu32rxMQPush;/*发送写入消息*/
    uint8_t u8rxbuff[USART_SIF2_MAX_RX_SIZE];	

    bool_t brxCompleteFlg;/*启动接收标志*/	
} stc_uart_sif2_t;

typedef struct stc_uart
{   
		stc_uart_log_t stcLog;	
    stc_uart_rs485_t stcRs485;
    stc_uart_sif1_t stcSif1;	
    stc_uart_sif2_t stcSif2;		
} stc_uart_t;

//----------------------------------------------------------------------------------------
// @Export global Variables declare in the module
extern stc_uart_t g_stcUart;

//----------------------------------------------------------------------------------------
// @Export declaration of functions in the module
void drv_usart_sendData(uint8_t UartIndex,uint8_t *p, uint32_t len);
void drv_usart_sleep(void);
void drv_usart_wakeup(void);
void drv_usart_timeout(void);

void drv_usart1_init(uint32_t baudrate);
void drv_usart3_init(uint32_t baudrate);
void drv_usart4_init(uint32_t baudrate);
void drv_usart5_init(uint32_t baudrate);
void drv_usart_init(void);

#endif

// =======================================================================================
// End of file.
// =======================================================================================
